#include <QVBoxLayout>
#include <QHeaderView>
#include "core/account.h"
#include "accountwidget.h"

AccountWidget::AccountWidget(QWidget *parent)
: QWidget(parent)
{
    m_accountWidget = new QTableWidget();
    m_accountWidget->setColumnCount(2);
    m_accountWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_accountWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_accountWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    m_accountWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    m_accountWidget->verticalHeader()->hide();
    m_accountWidget->horizontalHeader()->hide();
    m_accountWidget->clearContents();

    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(m_accountWidget);
    setLayout(layout);

    Account *account = Account::inst();
    account->addListener(this);
}

void AccountWidget::updateContents()
{
    m_accountWidget->clearContents();
    m_accountWidget->setRowCount(4);
    QTableWidgetItem *item00 = new QTableWidgetItem("Balance");
    QTableWidgetItem *item10 = new QTableWidgetItem("Equity");
    QTableWidgetItem *item20 = new QTableWidgetItem("Floating");
    QTableWidgetItem *item30 = new QTableWidgetItem("Points");
    m_accountWidget->setItem(0, 0, item00);
    m_accountWidget->setItem(1, 0, item10);
    m_accountWidget->setItem(2, 0, item20);
    m_accountWidget->setItem(3, 0, item30);

    Account *account = Account::inst();
    QTableWidgetItem *item01 = new QTableWidgetItem(QString::number(account->getAccountBalance(), 'g', 10));
    QTableWidgetItem *item11 = new QTableWidgetItem(QString::number(account->getAccountEquity(), 'g', 10));
    QTableWidgetItem *item21 = new QTableWidgetItem(QString::number(account->getAccountFloating(), 'g', 10));
    QTableWidgetItem *item31 = new QTableWidgetItem(QString::number(-account->getAccountFee(), 'g', 10));

    m_accountWidget->setItem(0, 1, item01);
    m_accountWidget->setItem(1, 1, item11);
    m_accountWidget->setItem(2, 1, item21);
    m_accountWidget->setItem(3, 1, item31);

    update();
}

void AccountWidget::onAccountChanged()
{
    updateContents();
}